package com.bupt.kgplatform.config.HandlerInterceptor;

import com.bupt.kgplatform.Util.RedisCache;
import com.bupt.kgplatform.Util.UserHolder;
import com.bupt.kgplatform.entity.User;
import com.bupt.kgplatform.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * LoginHandlerInterceptor
 *
 * @author zrz
 * @date 2024/5/20
 * @time 12:54
 * @Description 登录拦截器
 */
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头中的信息
        if (UserHolder.getUser() == null) {
            //用户不存在，返回状态码401并拦截
            response.setStatus(401);
            return false;
        }
        //放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //移除user，防止用户信息泄露
        UserHolder.removeUser();
    }

}
